package com.pb.infra.taskManage.service;

import com.pb.infra.oth.entity.common.Page;
import com.pb.infra.oth.entity.common.Result;
import com.pb.infra.taskManage.mapper.TaskControlMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 *任务监控
 * @author liaohaoyuan
 * @date 20210501
 */
@Service
public class TaskControlService {

    /**
     * DAO映射对象
     */
    @Resource
    private TaskControlMapper taskControlMapper;

    /**
     * 查询分页数据
     * @param map 查询参数
     * @return
     */
    public Result select(Map<String, Object> map,Page page){
        Integer pageIndex = Integer.parseInt(map.get("pageIndex").toString());
        Integer pageSize = Integer.parseInt(map.get("pageSize").toString());
        Integer pageEnd = pageSize * pageIndex;
        Integer pageStart = (pageIndex - 1) * pageSize;
        map.put("pageEnd", pageEnd);
        map.put("pageStart", pageStart);
        List<Map<String, Object>> list = taskControlMapper.select(map);
        return Result.ok(taskControlMapper.selectCount(map),list);
    }

    /**
     * 查询分页数据
     * @param map 查询参数
     * @return
     */
    public Result getLastDataDate(Map<String, Object> map){
        List<Map<String, Object>> list = taskControlMapper.getLastDataDate(map);
        return Result.ok(list);
    }

    /**
     * 修改工作状态
     * @return
     */
    public  Result doUpdateStatus(Map<String, Object> paramMap){
        List<Map<String, Object>> list;
        Result result = this.doVldBfReset(paramMap);
        if (result.getCode().equals("-1")){
            return result;
        }
        try{
            list = taskControlMapper.getCheckData(paramMap);
            if (list.size()>0){
                List<Map<String, Object>> list2 = taskControlMapper.getCheckData(paramMap);
                for (Map<String, Object> map : list2) {
                    if ("F".equals(map.get("jobStatus").toString())
                            ||map.get("jobStatus").toString().equals("F")){
                        map.put("jobStatus","W");
                        taskControlMapper.doUpdateStatus(map);
                    }
                }
            }
            paramMap = list.get(0);
            paramMap.put("jobStatus","P");
            taskControlMapper.doUpdateStatus(paramMap);
        }catch (Exception e){
            e.printStackTrace();
        }
        return Result.OK_EMPTY;
    }

    /**
     * 验证重置可行性
     * @param map
     * @return
     */
    public Result doVldBfReset(Map<String, Object> map) {
        List<Map<String, Object>> list ;
        try{
             list = taskControlMapper.getCheckData(map);
            if (list.size()<1){
                return Result.error("-1","无法重置，验证重置可行性时未找到任务信息，请检查MA_JOB_STATUS_SCHEDULE表!");
            }
            else {
                String status = list.get(0).get("jobStatus").toString();
                // 判断前端传过来的信息与数据库信息是否匹配
                if (status.equals(map.get("jobStatus").toString())){
                    List<String> strings = taskControlMapper.selectStatus(map);
                    String statuses = strings.toString();
                    if (statuses.contains("R")||statuses.contains("P")) {
                        return Result.error("-1"," 无法重置，该任务运行日期内存在正在运行或者等待运行的任务!");
                    }
                }else {
                    return Result.error("-1","任务状态已变更，请重新查询后再进行重置操作!");
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("-1","数据库异常，请联系维护人员维护");
        }
        return Result.ok("OK");
    }

}
